/**
 * Copyright (C) 2017-2018 pinganfang, Inc. All Rights Reserved.
 */
package com.pingan.haofang.searchcloud.solr.schema;

import java.io.StringWriter;
import java.util.Map;

import com.pingan.haofang.searchcloud.api.schema.IndexSchema;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.pingan.haofang.searchcloud.solr.constants.SolrConstant;


/**
 * 
 * @author huhailiang937
 * @date 2017年5月3日下午9:49:35
 * @since 1.0.0
 */
public class GeneraterEngine {

    protected static Logger LOG = LoggerFactory.getLogger(GeneraterEngine.class);

    public static final String VELOCTIY_CONFIG_KEY = "schemaData";

    private static VelocityEngine velocityEngine = null;

    private static VelocityEngine getVelocityEngine() {
        if (null != velocityEngine) {
            return velocityEngine;
        }

        velocityEngine = new VelocityEngine();
        velocityEngine.setProperty(Velocity.RESOURCE_LOADER, "class");
        velocityEngine.setProperty(Velocity.VM_LIBRARY, SolrConstant.SCHEMA_TEMPLET_ROOT + "commom.vm");
        velocityEngine.setProperty("class.resource.loader.class",
                "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        velocityEngine.setProperty(Velocity.OUTPUT_ENCODING, SolrConstant.GLOBAL_CHAR_SET_ENCODING);
        try {
            velocityEngine.init();
        } catch (Exception e) {
            LOG.error("Schema velocityEngine init error:", e);
            throw new RuntimeException("Schema velocityEngine init error:", e);
        }
        return velocityEngine;
    }

    public static String generator(Map<String, Object> modelDataMap, String templet) {

        VelocityEngine velocityEngine = getVelocityEngine();
        try {
            // 加载模板，设定模板编码
            Template template = velocityEngine.getTemplate(templet, SolrConstant.GLOBAL_CHAR_SET_ENCODING);
            VelocityContext context = new VelocityContext();

            if (null != modelDataMap && !modelDataMap.isEmpty()) {
                for (Map.Entry<String, Object> entry : modelDataMap.entrySet()) {
                    context.put(entry.getKey(), entry.getValue());
                }
            }
            // 设置输出
            StringWriter writer = new StringWriter();
            // 将环境数据转化输出
            template.merge(context, writer);
            return writer.toString();
        } catch (ResourceNotFoundException e) {
            LOG.error("VelocityEngine Schema ResourceNotFoundException:", e);
            throw new RuntimeException("VelocityEngine Schema ResourceNotFoundException:", e);
        } catch (ParseErrorException e) {
            LOG.error("VelocityEngine Schema ParseErrorException:", e);
            throw new RuntimeException("VelocityEngine Schema ParseErrorException:", e);
        } catch (Exception e) {
            LOG.error("VelocityEngine Schema Exception:", e);
            throw new RuntimeException("VelocityEngine Schema Exception:", e);
        }
    }

    public static String generator(IndexSchema modelData, String templet) {
        VelocityEngine velocityEngine = getVelocityEngine();
        try {
            // 加载模板，设定模板编码
            Template template = velocityEngine.getTemplate(templet, SolrConstant.GLOBAL_CHAR_SET_ENCODING);
            VelocityContext context = new VelocityContext();
            context.put(VELOCTIY_CONFIG_KEY, modelData);

            // 设置输出
            StringWriter writer = new StringWriter();
            // 将环境数据转化输出
            template.merge(context, writer);
            return writer.toString();
        } catch (ResourceNotFoundException e) {
            LOG.error("VelocityEngine generator Schema ResourceNotFoundException:", e);
            throw new RuntimeException("VelocityEngine Schema ResourceNotFoundException:", e);
        } catch (ParseErrorException e) {
            LOG.error("VelocityEngine generator Schema ParseErrorException:", e);
            throw new RuntimeException("VelocityEngine Schema ParseErrorException:", e);
        } catch (Exception e) {
            LOG.error("VelocityEngine Schema Exception:", e);
            throw new RuntimeException("VelocityEngine generator Schema Exception:", e);
        }
    }
}
